<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>投放号列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>


<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="listSearch">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <input type="text" name="account" placeholder="投放号名称" autocomplete="off" class="layui-input">
                </div>

                <div class="layui-inline">
                    <select name="status" lay-search>
                        <option value="-1">账户状态</option>
                        <option value="1">正常</option>
                        <option value="0">禁用</option>
                    </select>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-list-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>

            </div>
        </div>
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">添加</button>
            </div>
            <table id="LAY-list-manage" lay-filter="LAY-list-manage"></table>
            <script type="text/html" id="table-tool">
                <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="AuthQuick">授权</a>
                <a class="layui-btn layui-btn-primary layui-border-orange layui-btn-xs" lay-event="switchAccount">切换账号</a>
                <a class="layui-btn layui-btn-primary layui-border-green layui-btn-xs" lay-event="more" data-id="{{d.id}}">更多 <i class="layui-icon layui-icon-down"></i></a>
            </script>

            <script type="text/html" id="switchTpl">
                <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="正常|禁用" lay-filter="statusSwitch" {{ d.status == 1 ? 'checked' : '' }}>
            </script>
        </div>
    </div>
</div>


<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','table','admin','dropdown'], function(){

        var $ = layui.$
            ,form = layui.form
            ,admin = layui.admin
            ,table = layui.table
            ,dropdown = layui.dropdown;

        //监听搜索
        form.on('submit(LAY-list-front-search)', function(data){
            var field = data.field;
            //执行重载
            table.reload('LAY-list-manage', {
                where: field
                ,page: {
                    curr: 1  // 将页码重置为第一页
                }
            });
        });


        form.on('switch(statusSwitch)', function (data) {
            var status = 1;
            if (data.elem.checked === false) {
                var status = 0;
            }

            $.ajax({
                url:"{:url('ManagerUser/disable')}",
                type:"post",
                dataType: "json",
                data: {'id': data.value, 'status': status},
                async:true,
                success: function(rs){
                    if(rs['code'] == 100){
                        layer.msg('设置成功', {icon: 1, time: 1000}, function () {
                        });
                    }else{
                        layer.msg(rs['msg']);
                    }
                    return false;
                }
            });
        });


        //书籍管理
        table.render({
            elem: '#LAY-list-manage'
            ,url: "{:url('Agent/channel_data')}" //模拟接口
            ,autoSort: false
            , response: {
                statusCode: 1 //规定成功的状态码，本来是：0，现在统一改为1
            }
            ,cols: [[
                {field: 'id', width: 120, title: '投放号ID', sort: true, align:'center'}
                ,{field: 'account', title: '账户名称', align:'center'}
                ,{field: 'channel_code', title: '渠道标识'}
                ,{field: 'scale', title: '内容等级'}
                ,{field: 'sharing_ratio', title: '分成比例', align:'center'}
                ,{field: 'quicks', title: '已授权App', align:'center'}
                ,{field: 'status', title: '状态', width: 100, templet: '#switchTpl', unresize: true, align: 'center'}
                ,{field: 'create_time', title: '创建时间', align:'center'}
                ,{title: '操作', width: 350, align:'center', fixed: 'right', toolbar: '#table-tool'}
            ]]
            ,page: true
            ,limit: 30
            ,height: 'full-220'
            ,text: '对不起，加载出现异常！'
        });

        //触发排序事件
        table.on('sort(LAY-list-manage)', function(obj){

            table.reload('LAY-list-manage', {
                initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
                ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    field: obj.field //排序字段
                    ,order: obj.type //排序方式
                }
            });

        });

        //监听工具条
        table.on('tool(LAY-list-manage)', function(obj){

            if(obj.event === 'switchAccount'){

                $.ajax({
                    url: "{:url('ManagerUser/switch_account')}",
                    type: "post",
                    dataType: "json",
                    data: {id:obj.data.id},
                    async: true,
                    success: function (rs) {
                        if (rs['code'] == 100) {
                            parent.location.href = '/';
                        } else {
                            layer.msg(rs['msg']);
                        }
                        return false;
                    }
                });

            }
            else if(obj.event === 'AuthQuick'){
                layer.open({
                    type: 2
                    ,title: obj.data.account + '-授权App'
                    ,offset: '15px'
                    ,content: '/agent/empowerApp.html?id=' + obj.data.id
                    ,area: ['615px', '500px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        layer.confirm('确认修改吗？授权后App将不能取消授权！', {
                            btn: ['确定', '取消'] //按钮
                        }, function () {

                            var iframeWindow = window['layui-layer-iframe'+ index]
                                ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");
                            //监听提交
                            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function (data) {
                                var field = data.field; //获取提交的字段
                                admin.req({
                                    url: '/agent/empowerApp'
                                    , type: "POST"
                                    , data: field
                                    , done: function (res) {
                                        layer.msg('设置成功', {icon: 1, time: 1000}, function () {
                                            table.reload('LAY-list-manage');
                                            layer.close(index); //关闭弹层
                                        });
                                    }, not_finish: function (res) {
                                        layer.msg(res.msg);
                                    }
                                });
                            });
                            submit.trigger('click');

                        }, function () {

                        });
                    }
                    ,success: function(layero, index){

                    }
                })
            }
            else if(obj.event === 'resetPwd'){
                layer.open({
                    type: 2
                    ,title: '重置密码'
                    ,content: "{:url('ManagerUser/reset_pwd')}?id="+obj.data.id
                    ,maxmin: true
                    ,area: ['390px', '300px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-reset-pwd-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){

                            $.ajax({
                                url:"{:url('ManagerUser/reset_pwd')}",
                                type:"post",
                                dataType: "json",
                                data:data.field,
                                async:true,
                                success: function(rs){
                                    if(rs['code'] == 100){

                                        layer.msg('密码重置成功',function () {
                                            layer.close(index); //关闭弹层
                                        });

                                    }else{
                                        layer.msg(rs['msg']);
                                    }
                                    return false;
                                }
                            });
                        });

                        submit.trigger('click');
                    }
                });
            } else if (obj.event === 'more') {
                dropdown.render({
                    elem: this
                    , show: true //外部事件触发即显示
                    , data: [{
                        title: '重置密码'
                        , id: 'resetPwd'
                    }, {
                        title: '同步充值模版'
                        , id: 'tbu'
                    }]
                    , click: function (data, othis) {
                        //根据 id 做出不同操作
                        var elem = $(this.elem)
                            ,listId = elem.data('id'); //表格列表的预埋数据
                        if (data.id === 'resetPwd') {
                            layer.open({
                                type: 2
                                ,title: '重置密码'
                                ,content: "{:url('ManagerUser/reset_pwd')}?id="+obj.data.id
                                ,maxmin: true
                                ,area: ['390px', '300px']
                                ,btn: ['确定', '取消']
                                ,yes: function(index, layero){
                                    var iframeWindow = window['layui-layer-iframe'+ index]
                                        ,submitID = 'LAY-reset-pwd-submit'
                                        ,submit = layero.find('iframe').contents().find('#'+ submitID);

                                    //监听提交
                                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){

                                        $.ajax({
                                            url:"{:url('ManagerUser/reset_pwd')}",
                                            type:"post",
                                            dataType: "json",
                                            data:data.field,
                                            async:true,
                                            success: function(rs){
                                                if(rs['code'] == 100){

                                                    layer.msg('密码重置成功',function () {
                                                        layer.close(index); //关闭弹层
                                                    });

                                                }else{
                                                    layer.msg(rs['msg']);
                                                }
                                                return false;
                                            }
                                        });
                                    });
                                    submit.trigger('click');
                                }
                            });
                        }else if (data.id === 'tbu'){
                            layer.open({
                                type: 2
                                ,title: '来源投放号'
                                ,content: '/recharge/syncPayItem.html?id=' + obj.data.id
                                ,area: ['500px', '400px']
                                ,btn: ['确定', '取消']
                                ,yes: function(index, layero){
                                    var iframeWindow = window['layui-layer-iframe'+ index]
                                        ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

                                    //监听提交
                                    iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
                                        var field = data.field; //获取提交的字段

                                        //提交 Ajax 成功后，静态更新表格中的数据
                                        //$.ajax({});
                                        admin.req({
                                            url: '/recharge/syncPayItem'
                                            , type: "POST"
                                            , data: field
                                            , done: function (res) {
                                                layer.msg('同步成功', {icon: 1, time: 1000}, function () {
                                                    layer.close(index); //关闭弹层
                                                });
                                            },not_finish:function (res) {
                                                layer.msg(res.msg);
                                            }
                                        });
                                    });

                                    submit.trigger('click');
                                }
                                ,success: function(layero, index){

                                }
                            })
                        }
                    }
                    , align: 'right' //右对齐弹出（v2.6.8 新增）
                    , style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
                });

            }
        });

        var active = {
            add: function(){
                layer.open({
                    type: 2
                    ,title: '添加投放号'
                    ,content: "{:url('Agent/channel_add')}"
                    ,maxmin: true
                    ,area: ['540px', '420px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-data-add-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);

                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){

                            $.ajax({
                                url:"{:url('Agent/channel_add')}",
                                type:"post",
                                dataType: "json",
                                data:data.field,
                                async:true,
                                success: function(rs){
                                    if(rs['code'] == 100){

                                        layer.msg('投放号添加成功',function () {
                                            layer.close(index); //关闭弹层
                                            table.reload('LAY-list-manage');
                                        });

                                    }else{
                                        layer.msg(rs['msg']);
                                    }
                                    return false;
                                }
                            });
                        });

                        submit.trigger('click');
                    }
                });
            }
        };

        $('.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>
</body>
</html>